import Vue from 'vue'
import Vuex from 'vuex'
import {getTimeInterval} from '../utils/index'
import {fetchSocial,fetchSiteInfo} from '../api'

//引入刚刚定义好的 storage
import storage from "../model/storage";


Vue.use(Vuex)
// 略:后台获取系统运行时间
const runAt = '1589878800000';
let timer = null;

const store = new Vuex.Store({
    state: {
        loading: false,
        //存放token，username,uid
        token: '',
        uid: '',
        userName: "",
        runTimeInterval: '',
        socials: '',
        websiteInfo: ''
    },
    mutations: {
        SET_LOADING: (state, v) => {
            state.loading = v;
        },
        SET_SOCIALS: (state, v) => {
            state.socials = v;
        },
        SET_SITE_INFO: (state, v) => {
            state.websiteInfo = v;
        },
        GET_RUNTIME_INTERVAL: (state) => {
            if (!timer || !state.runTimeInterval) {
                clearInterval(timer)
                timer = setInterval(() => {
                    state.runTimeInterval = getTimeInterval(runAt);
                }, 1000);
            }
        },
        // 修改token，并将token存入localStorage
        set_token(state, token) {
            state.token = token;
            storage.set('token', token);
            console.log('store、localstorage保存token成功！');
        },
        del_token(state) {
            state.token = "";
            storage.remove("token");
            console.log('store、localstorage删除token成功！');
        },
        // 可选
        setUserInfo(state, userName) {
            state.userName = userName;
            storage.set('username', userName);
            console.log('store、localstorage保存username成功！');
        },
        del_userInfo(state) {
            state.userName = ""
            storage.remove('username')
            console.log('store、localstorage删除username成功！');
        },
        //设置uid
        setUserID(state, uid) {
            state.uid = uid;
            storage.set('uid', uid)
            console.log('store、localstorage保存UID成功！');
        },
        delUserID(state) {
            state.uid = ""
            storage.remove('uid')
            console.log('store、localstorage删除UID成功！');
        }
    },
    actions: {
        setLoading: ({commit}, v) => {
            commit('SET_LOADING', v);
        },
        setToken:({commit},v)=>{
            commit('set_token',v)
        },
        setUserName:({commit},v)=>{
            commit('setUserInfo',v)
        },
        setUID:({commit,v})=>{
            commit('setUserID',v)
        },
        initComputeTime: ({commit}) => {
            commit('GET_RUNTIME_INTERVAL');
        },
        getSiteInfo: ({commit, state}) => {
            return new Promise(resolve => {
                if (state.websiteInfo) {
                    resolve(state.websiteInfo)
                } else {
                    fetchSiteInfo().then(res => {
                        let data = res.data || {}
                        commit('SET_SITE_INFO', data);
                        resolve(data);
                    }).catch(err => {
                        resolve({});
                    })
                }
            })
        },
        getSocials: ({commit, state}) => {
            return new Promise((resolve => {
                if (state.socials) {
                    resolve(state.socials)
                } else {
                    fetchSocial().then(res => {
                        let data = res.data || []
                        commit('SET_SOCIALS', data);
                        resolve(data);
                    }).catch(err => {
                        resolve([]);
                    })
                }
            }))
        }
    },
    getters: {
        loading: state => state.loading,
        runTimeInterval: state => state.runTimeInterval,
        notice: state => state.websiteInfo ? state.websiteInfo.notice : '',
        username: state => state.userName
    },

})

export default store;
